package com.hangzhou.xyj.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.hangzhou.xyj.utils.StringUtil;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import java.util.Date;
import java.util.UUID;

/**
 * Created by NKY on 10/10.
 */
@DatabaseTable
public class XXLocation implements Parcelable {

    public static final Creator<XXLocation> CREATOR
            = new Creator<XXLocation>() {
        public XXLocation createFromParcel(Parcel in) {
            return new XXLocation(in);
        }

        public XXLocation[] newArray(int size) {
            return new XXLocation[size];
        }
    };
    @DatabaseField
    public String city;
    @DatabaseField
    public String district;
    @DatabaseField
    public String detail;
    @DatabaseField
    public String province;
    @DatabaseField
    public Date updateDate = new Date();
    /**
     * 应用的业务逻辑城市编码,自己的服务器生成的值
     */
    @DatabaseField
    public String cityCode;
    /**
     * 地图定位的原始的城市编码(行政编码)
     */
    @DatabaseField
    public String originCityCode;
    @DatabaseField(generatedId = true)
    private UUID id;
    @DatabaseField
    private double latitude;
    @DatabaseField
    private double longitude;
    @DatabaseField
    private String address;

    public XXLocation() {
    }

    public XXLocation(double latitude, double longitude, String address) {
        this.latitude = latitude;
        this.longitude = longitude;
        this.address = address;
    }


    public XXLocation(Parcel in) {
        latitude = in.readDouble();
        longitude = in.readDouble();
        address = in.readString();
        cityCode = in.readString();
        district = in.readString();
        province = in.readString();
        updateDate = new Date(in.readLong());
        city = in.readString();
        originCityCode = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeDouble(latitude);
        dest.writeDouble(longitude);
        dest.writeString(address);
        dest.writeString(cityCode);
        dest.writeString(district);
        dest.writeString(province);
        dest.writeLong(updateDate.getTime());
        dest.writeString(city);
        dest.writeString(originCityCode);
    }

    @Override
    public String toString() {
        return "XXLocation{" +
                "latitude=" + latitude +
                ", longitude=" + longitude +
                ", address='" + address + '\'' +
                ", city='" + city + '\'' +
                ", district='" + district + '\'' +
                ", detail='" + detail + '\'' +
                ", province='" + province + '\'' +
                ", updateDate='" + updateDate + '\'' +
                ", cityCode='" + cityCode + '\'' +
                ", originCityCode='" + originCityCode + '\'' +
                '}';
    }


    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    /**
     * 获取城市，去掉"市"
     *
     * @return
     */
    public String getCity() {
        if (StringUtil.isNotBlank(city)) {
            if (city.contains("市") && city.indexOf("市") == city.length() - 1)
                city = city.substring(0, city.length() - 1);
        }
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getCityCode() {
        return cityCode;
    }

    public void setCityCode(String cityCode) {
        this.cityCode = cityCode;
    }

    public String getOriginCityCode() {
        return originCityCode;
    }

    public void setOriginCityCode(String originCityCode) {
        this.originCityCode = originCityCode;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getDetailAddress() {
        StringBuilder sb = new StringBuilder();
        sb.append(province)
                .append(city)
                .append(district)
                .append(address);

        return sb.toString();
    }
}
